﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlogEngine.Core;
using System.Web.UI;
using _4mvcBlog.Core;
using _4mvcBlog.Core.ViewEngine;
using System.Web.UI.WebControls;

namespace _4mvcBlog.Controllers
{
    public partial class AdminController
    {
        #region 分类管理

        /// <summary>
        /// 显示分类管理页面
        /// </summary>
        public ActionResult CategorysManage()
        {
            List<Category> categorys = Category.Categories;
            return View(categorys);
        }

        /// <summary>
        /// 添加分类
        /// </summary>
        [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Put)]
        [TestUserAuthorize]
        public ActionResult AddCategory([Bind(Prefix = "", Include = "Title,Description")] Category category)
        {
            if (!ViewData.ModelState.IsValid)
            {
                return View("NewCategory", category);
            }

            if (!category.IsValid)
            {
                foreach (string key in category.BrokenRules.Keys)
                {
                    ViewData.ModelState.AddModelError(key, category.BrokenRules[key]);
                }
                return View("NewCategory", category);
            }

            category.Save();

            return RedirectToAction("CategorysManage");
        }

        /// <summary>
        /// 显示编辑分类页面
        /// </summary>
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult EditCategory(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return ShowMsg("请指定要编辑的分类的Id");
            }

            Guid guid = new Guid(id);
            BlogEngine.Core.Category category = BlogEngine.Core.Category.GetCategory(guid);
            if (null == category)
            {
                return ShowMsg("你要编辑的分类不存在");
            }

            return View(category);
        }

        /// <summary>
        /// 更新分类数据到数据库
        /// </summary>
        [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Put)]
        [TestUserAuthorize]
        public ActionResult UpdataCategory(FormCollection form, string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return ShowMsg("请指定要编辑的分类的Id");
            }

            Guid guid;
            BlogEngine.Core.Category category;
            try
            {
                guid = new Guid(id);
                category = BlogEngine.Core.Category.GetCategory(guid);
            }
            catch (Exception ex)
            {
                return ShowMsg(new List<string>() { ex.Message });
            }

            if (null == category)
            {
                return ShowMsg("你要编辑的分类不存在");
            }

            try
            {
                UpdateModel(category, new[] { "Title", "Description" });
            }
            catch
            {
                ViewData.ModelState["Id"] = null;
                return View("EditCategory", category);
            }

            if (!category.IsValid)
            {
                foreach (string key in category.BrokenRules.Keys)
                {
                    ViewData.ModelState.AddModelError(key, category.BrokenRules[key]);
                }
                ViewData.ModelState["Id"] = null;
                return View("EditCategory", category);
            }

            category.Save();

            return RedirectToAction("CategorysManage");
        }

        /// <summary>
        /// 删除分类
        /// </summary>
        [TestUserAuthorize]
        public ActionResult DelCategory(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return ShowMsg("请指定要删除的分类的Id");
            }
            try
            {
                Guid guid = new Guid(id);
                BlogEngine.Core.Category category = BlogEngine.Core.Category.GetCategory(guid);
                category.Delete();
                category.Save();
                return ShowMsg("删除分类《" + category.Title + "》成功");
            }
            catch (Exception ex)
            {
                return ShowMsg(ex.Message);
            }
        }

        #endregion
    }
}
